package com.bbc.yuntun.framwork.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * mybatis-plus 自动注入
 *
 * @author liac
 * @date 2022/4/6
 */
@Slf4j
@Component
public class DefaultMetaObjectHandler implements MetaObjectHandler {
    private static final String UPDATE_TIME = "updateTime";
    private static final String CREATE_TIME = "createTime";
    private static final String CREATE_BY = "createBy";
    private static final String UPDATE_BY = "updateBy";
    private static final String VERSION = "version";
    private static final String DEL_FLAG = "delFlag";

    private static final Integer DEFAULT_VERSION = 1;
    private static final Integer DEFAULT_DEL_FLAG = 0;

    public DefaultMetaObjectHandler() {
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = null;
        try {
           // userId = SecurityUtils.getLoginUser().getUserId();
        } catch (Exception var10) {
//            log.warn("未查找到用户信息");
        }
        Long finalUserId = userId;
        this.strictInsertFill(metaObject, CREATE_BY, () -> finalUserId, Long.class);
        this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, UPDATE_BY, () -> finalUserId, Long.class);
        this.strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, VERSION, () -> DEFAULT_VERSION, Integer.class);
        this.strictInsertFill(metaObject, DEL_FLAG, () -> DEFAULT_DEL_FLAG, Integer.class);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = null;
        try {
            // userId = SecurityUtils.getLoginUser().getUserId();
        } catch (Exception var10) {
//            log.warn("未查找到用户信息");
        }
        Long finalUserId = userId;
        this.strictUpdateFill(metaObject, UPDATE_BY, () -> finalUserId, Long.class);
        this.strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
    }
}
